﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;


/// <summary>
/// Scrum web control for selection one of 5 priorities.
/// </summary>
public partial class PrioritySelection : ObjectSelection
{
    protected override WebControl GetWebControl()
    {
        return this.Priorities;
    }

    protected override Label GetLabel()
    {
        return this.FieldLabel;
    }

    /// <summary>
    /// boundary for maximal priority (including)
    /// </summary>
    protected const int maxPriority = 4;

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPriorities();
    }

    /// <summary>
    /// Loads the priorities.
    /// </summary>
    private void LoadPriorities()
    {
        if (Priorities.Items.Count == 0)
        {
            for (int i = 0; i <= maxPriority; i++)
            {
                Priorities.Items.Add(GetPriorityListItem(i));
            }
            if (this.IsFilter)
            {
                Priorities.Items.Insert(0, this.TopListItemForFilter(ObjectSelectionType.Priority.ToString()));
            }
        }
    }

    /// <summary>
    /// Gets the priority represented by ListItem.
    /// </summary>
    /// <param name="priority">The priority.</param>
    /// <returns></returns>
    private ListItem GetPriorityListItem(int priority)
    {
        string priorityValue = priority.ToString();
        string priorityResourceKey = String.Format("Priority{0}", priorityValue);
        string priorityName = (string)GetGlobalResourceObject(ObjectSelectionType.Priority.ToString(), priorityResourceKey);
        return new ListItem(priorityName, priorityValue);
    }

    /// <summary>
    /// Gets or sets the selected priority.
    /// </summary>
    /// <value>
    /// The selected priority. Value can be from range (0-infinity). 
    /// Priority bigger than 5 is represented as highest
    /// </value>
    public int SelectedPriority
    {
        get
        {
            return Int32.Parse(Priorities.SelectedValue);
        }
        set
        {
            LoadPriorities();
            int v = value > maxPriority ? maxPriority : value;
            foreach (ListItem item in this.Priorities.Items)
            {
                item.Selected = false;
            }
            this.Priorities.Items.FindByValue(v.ToString()).Selected = true;
        }
    }

    /// <summary>
    /// Gets or sets the width.
    /// </summary>
    /// <value>The width.</value>
    [DefaultValue(100)]
    public int Width { get; set; }


    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ScrumInput"/> is editable.
    /// </summary>
    /// <value><c>true</c> if editable; otherwise, <c>false</c>.</value>
    //public bool Enabled
    //{
    //    get
    //    {
    //        return this.Priorities.Enabled;
    //    }
    //    set
    //    {
    //        this.Priorities.Enabled = value;
    //    }
    //}

}
